package cn.com.surker.workcon.service.zhcl;

import cn.com.surker.base.system.dao.SysStorageLogDao;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.base.system.entity.SysStorageLog;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.lab.entity.LabExpendOther;
import cn.com.surker.util.DateUtil;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ManualService;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手动配料 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ManualServiceImpl extends GKJDBCService implements ManualService {

	@Resource
	private SysStorageLogDao sysStorageLogDao;

	@Override
	public List<LabExpendOther> findList(SysLine line, Date startTime) {
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String manualTalbe = ZhclUtil.getRecordTableName(line, "N", "M");
		String materialTable = ZhclUtil.getCtrlTableName(line, "Material");
		String storeHouseTable = ZhclUtil.getCtrlTableName(line, "StoreHouse");
		//	查询sql
		String sql = "  SELECT a.BNo,a.Dkt,a.Btch,a.BalID,a.MCode,a.ECode,a.EName,a.BinCode,a.MNotes,a.InputDesc,a.RecTm,a.OpenTs,a.StartW,a.FinishW,a.DisW,a.Oper,a.DealUser,a.Invo_ID,a.Flag1,a.Flag2,a.Flag3,a.StNo,a.ErpNo,a.RFlag,a.SysGuid,a.ProGuid,\n" +
			"  b.Name materialName,c.Name storeName\n" +
			"  FROM " + manualTalbe + " a\n" +
			"  left join " + materialTable + " b ON a.MCode = b.Code\n" +
			"  left join " + storeHouseTable + " c ON a.MCode = c.LoadMaterial\n" +
			"  where a.RecTm > ?";
		Object[] params = {startTime};
		List<Map<String, Object>> bcsDataList = lineJdbcTemp.queryForList(sql, params);
		if (bcsDataList == null || bcsDataList.isEmpty()) {
			return null;
		}
		Long lineId = line.getId();
		//	转换数据
		List<LabExpendOther> labExpendOtherList = new ArrayList<>();
		for (int i = 0, s = bcsDataList.size(); i < s; i++) {
			Map<String, Object> bcsData = bcsDataList.get(i);
			//	工控料仓名
			String bcsStorage = String.valueOf(bcsData.get("storeName"));
			//	工控材料名
			String bcsMaterial = String.valueOf(bcsData.get("materialName"));
			//	下料时间
			Date datTim = DateUtil.convertDate(String.valueOf(bcsData.get("RecTm")));
			String taskTime = DateUtil.convertString(datTim);
			//	操作工
			String operator = String.valueOf(bcsData.get("Oper"));
			//	操作数量
			Double amount = Double.valueOf(String.valueOf(bcsData.get("DisW")));
			//	根据工控数据,查询erp的仓位
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, bcsMaterial, 1, taskTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			//	创建erp手动消耗数据
			LabExpendOther labExpendOther = new LabExpendOther();
			labExpendOther.setLineId(lineId);
			labExpendOther.setMaterialId(erpMaterialId);
			labExpendOther.setStorageId(erpStorageId);
			labExpendOther.setDosage(amount);
			labExpendOther.setStatus(StateEnum.ENABLED.getCode());
			labExpendOther.setRemark(operator);
			labExpendOther.setCreator(bcsCreater);
			labExpendOther.setExpendTime(datTim);
			labExpendOther.setCreateTime(new Date());
			labExpendOther.setControlMaterial(bcsMaterial);
			labExpendOther.setControlStorage(bcsStorage);
			labExpendOtherList.add(labExpendOther);
		}
		return labExpendOtherList;
	}

	@Override
	public void updateFlag(SysLine line, Date startTime) {
	}

	@Override
	public List<LabExpendOther> findAllList(Long lineId, String startTime, String endTime) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String manualTalbe = ZhclUtil.getRecordTableName(line, "N", "M");
		String materialTable = ZhclUtil.getCtrlTableName(line, "Material");
		String storeHouseTable = ZhclUtil.getCtrlTableName(line, "StoreHouse");
		//	查询sql
		String sql = "  SELECT a.BNo,a.Dkt,a.Btch,a.BalID,a.MCode,a.ECode,a.EName,a.BinCode,a.MNotes,a.InputDesc,a.RecTm,a.OpenTs,a.StartW,a.FinishW,a.DisW,a.Oper,a.DealUser,a.Invo_ID,a.Flag1,a.Flag2,a.Flag3,a.StNo,a.ErpNo,a.RFlag,a.SysGuid,a.ProGuid,\n" +
			"  b.Name materialName,c.Name storeName\n" +
			"  FROM " + manualTalbe + " a\n" +
			"  left join " + materialTable + " b ON a.MCode = b.Code\n" +
			"  left join " + storeHouseTable + " c ON a.MCode = c.LoadMaterial\n" +
			"  where a.RecTm >= ? and a.RecTm < ?";
		Object[] params = {startTime, endTime};
		List<Map<String, Object>> bcsDataList = lineJdbcTemp.queryForList(sql, params);
		if (bcsDataList == null || bcsDataList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<LabExpendOther> labExpendOtherList = new ArrayList<>();
		for (int i = 0, s = bcsDataList.size(); i < s; i++) {
			Map<String, Object> bcsData = bcsDataList.get(i);
			//	工控料仓名
			String bcsStorage = String.valueOf(bcsData.get("storeName"));
			//	工控材料名
			String bcsMaterial = String.valueOf(bcsData.get("materialName"));
			//	下料时间
			Date datTim = DateUtil.convertDate(String.valueOf(bcsData.get("RecTm")));
			String taskTime = DateUtil.convertString(datTim);
			//	操作工
			String operator = String.valueOf(bcsData.get("Oper"));
			//	操作数量
			Double amount = Double.valueOf(String.valueOf(bcsData.get("DisW")));
			//	根据工控数据,查询erp的仓位
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, bcsMaterial, 1, taskTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			//	创建erp手动消耗数据
			LabExpendOther labExpendOther = new LabExpendOther();
			labExpendOther.setLineId(lineId);
			labExpendOther.setMaterialId(erpMaterialId);
			labExpendOther.setStorageId(erpStorageId);
			labExpendOther.setDosage(amount);
			labExpendOther.setStatus(StateEnum.ENABLED.getCode());
			labExpendOther.setRemark(operator);
			labExpendOther.setCreator(bcsCreater);
			labExpendOther.setExpendTime(datTim);
			labExpendOther.setCreateTime(new Date());
			labExpendOther.setControlMaterial(bcsMaterial);
			labExpendOther.setControlStorage(bcsStorage);
			labExpendOtherList.add(labExpendOther);
		}
		return labExpendOtherList;
	}
}
